package com.sysmanage.etl.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtils {
	private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 根据日期生成文件路径
	 * 
	 * @param fileMoudel
	 *            文件类型
	 * @param fileExtName
	 *            文件扩展名
	 * @return
	 */
	public static String genFilePath(String fileExtName) {
		String filePath = "";
		Date date = new Date();
		filePath += format.format(date);

		filePath = filePath.substring(0, filePath.length() - 2) + "/"
				+ filePath.substring(filePath.length() - 2);

		String rootPath = PropertiesUtils.getProperty("upload.file.path");
		File file = new File(rootPath + filePath);
		if (!file.exists()) {
			file.mkdirs();
		}

		fileExtName = fileExtName.startsWith(".") ? fileExtName : "."
				+ fileExtName;
		filePath += "/" + date.getTime() + RandomUtils.generateString(5)
				+ fileExtName;
		return filePath;
	}

	public static String getBasePath() {
		return PropertiesUtils.getProperty("upload.file.path");
	}

	/**
	 * 保存文件到指定目录
	 * 
	 * @param filePath
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public static Boolean saveFile(String filePath, byte[] data)
			throws IOException {
		if (StringUtils.isBlank(filePath) || data == null || data.length <= 0) {
			return false;
		}
		String fileSrc = PropertiesUtils.getProperty("upload.file.path")
				+ filePath;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(fileSrc));
			fos.write(data);
			fos.flush();
		} finally {
			if (fos != null)
				fos.close();
		}
		return true;
	}

	/**
	 * 读取输入流中的文件数据
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static byte[] getImageBytes(InputStream in) throws Exception {
		byte[] bytes = new byte[in.available()];
		in.read(bytes);
		in.close();
		return bytes;
	}

	/**
	 * 获取服务器上本地文件
	 * 
	 * @param photoPath
	 * @return
	 */
	public static File getFile(String filePath) {
		String fileSrc = PropertiesUtils.getProperty("upload.file.path")
				+ filePath;
		File file = null;
		try {
			file = new File(fileSrc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	/**
	 * 获取服务器本地文件数据流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] getFileByte(String filePath) {
		String fileSrc = PropertiesUtils.getProperty("upload.file.path") + "/"
				+ filePath;
		FileInputStream in = null;
		byte[] bytes = null;
		try {
			in = new FileInputStream(new File(fileSrc));
			bytes = new byte[in.available()];
			in.read(bytes);
		} catch (Exception e) {
			try {
				fileSrc = PropertiesUtils.getProperty("upload.file.path") + "/"
						+ filePath;
				in = new FileInputStream(new File(fileSrc));
				bytes = new byte[in.available()];
				in.read(bytes);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}

	public static void deletFile(String filePath) {
		File file = new File(PropertiesUtils.getProperty("upload.file.path")
				+ "/" + filePath);
		if (file.exists() && file.isFile())
			file.delete();
	}

}
